//
//  YMReweetView.swift
//  YMWeiBo
//
//  Created by mimi on 16/7/24.
//  Copyright © 2016年 mimi. All rights reserved.
//

import UIKit
import SnapKit
class YMReweetView: UIView {
    //记录转发微博的地步约束
    var retweetViewBottomConst: Constraint?
    
    
//恭外界赋值
    var statusViewModel:YMStatusViewModel?{
        
        didSet{
        contentLabel.text = statusViewModel?.status?.retweeted_status?.text
        
            /*
             
             配图赋值及逻辑
             有配图:
              首先要卸载底部约束
              刷新约束
              显示配图
             没配图:
              刷新约束
              影藏配图
             
             */
        
        self.retweetViewBottomConst?.uninstall()
            
            if statusViewModel?.status?.retweeted_status?.pic_urls?.count > 0 {
                
                //代表有值,先赋值
                pictureView.picuserInfo = statusViewModel?.status?.retweeted_status?.pic_urls
                
                //而约束更新
                self.snp_makeConstraints(closure: { (make) in
                 self.retweetViewBottomConst = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                pictureView.hidden = false
            }else{
                //代表无图
                self.snp_makeConstraints(closure: { (make) in
                self.retweetViewBottomConst = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                pictureView.hidden = true
            }
        
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI() {
        addSubview(contentLabel)
        addSubview(pictureView)
        contentLabel.snp_makeConstraints { (make) in
            make.width.equalTo(SCREENW-20)
            make.top.leading.equalTo(10)
        }
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.left.equalTo((SCREENW-250)/2)
        }
        
        
        self.snp_makeConstraints { (make) in
           self.retweetViewBottomConst = make.bottom.equalTo(contentLabel).offset(10).constraint
        }
    }

    
    //懒加载控件
    
    private lazy var contentLabel:UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFontOfSize(15)
        lab.textColor = UIColor.darkGrayColor()
        lab.numberOfLines = 0
        lab.preferredMaxLayoutWidth = SCREENW - 20
        
        return lab
        
    }()
    
    //配图view
    private lazy var pictureView:YMStatusPictureView = YMStatusPictureView()
}
